﻿using UnityEngine;
using YooAsset;
using ZFramework.Runtime;
using ZFramework.Runtime.DataCenter;
using ZFramework.Runtime.Download;
using ZFramework.Runtime.WebRequest;

namespace ZGame
{
    public static class GameApp
    {
        public static void Init()
        {
            Log.Error("开始  gameapp");
            AOTGameEntry.resourceManager.LoadAssetAsync<GameObject>("Assets/Res/GameApp/GameApp.prefab", new LoadAssetCallbacks(LoadAssetSuccessCallback, LoadAssetFailureCallback));
            //InitMgr();
        }

        public static void DeInit()
        {
            DeInitMgr();
        }

        private static void InitMgr()
        {
            GameEntry.baseMgr = FrameworkManagerManager.GetManager<BaseMgr>();
            GameEntry.eventMgr = FrameworkManagerManager.GetManager<EventManager>();
            GameEntry.webRequestManager = FrameworkManagerManager.GetManager<WebRequestManager>();
            GameEntry.downloadManager = FrameworkManagerManager.GetManager<DownloadManager>();
            GameEntry.fsmManager = FrameworkManagerManager.GetManager<FsmManager>();
            GameEntry.procedureManager = FrameworkManagerManager.GetManager<ProcedureManager>();
            GameEntry.objectPoolMgr = FrameworkManagerManager.GetManager<ObjectPoolMgr>();
            GameEntry.resourceManager = FrameworkManagerManager.GetManager<YooResourceMgr>();
            GameEntry.dataCenterManager = FrameworkManagerManager.GetManager<DataCenterManager>();
            GameEntry.entityMgr = FrameworkManagerManager.GetManager<EntityMgr>();
            GameEntry.itemMgr = FrameworkManagerManager.GetManager<ItemMgr>();
            GameEntry.sceneMgr = FrameworkManagerManager.GetManager<SceneMgr>();
            GameEntry.gameWorldManager = FrameworkManagerManager.GetManager<GameWorldManager>();
            GameEntry.uiMgr = FrameworkManagerManager.GetManager<FguiUIMgr>();
            GameEntry.fguiUIPackageMgr = FrameworkManagerManager.GetManager<FguiUIPackageMgr>();
            GameEntry.networkManager = FrameworkManagerManager.GetManager<NetworkManager>();
            GameEntry.netMessageMgr = FrameworkManagerManager.GetManager<NetMessageMgr>();
        }

        private static void DeInitMgr()
        {
            
        }

        private static void LoadAssetSuccessCallback(string entityAssetName, Object entityAsset, float duration,
            object userData)
        {
            Log.Error("加载成功");
            var go = Object.Instantiate(entityAsset);
            go.name = "GameApp";
            InitMgr();
        }
        
        private static void LoadAssetFailureCallback(string assetName, string errorMessage, object userData)
        {
            Log.Error("加载hotfix gameapp 失败 path=={0}, error=={1}", assetName, errorMessage);
        }

    }
}